<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="logo" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <!-- 主体区域 -->
    <el-container>

      <!-- 侧边导航条 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 折叠按钮 -->
        <div class="toggle-button" @click="toggleCollapse">
          <i class="el-icon-guide"></i>
        </div>

        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409BFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <el-submenu v-for="item in menulist" :key="item.id" :index="item.id + ''">
            <template slot="title">
              <i class="el-icon-folder-opened"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item :index="'/'+items.path" v-for="items in item.children" :key="items.id" @click="saveNavState('/'+items.path)">
              <i class="el-icon-folder"></i>
              <span slot="title">{{items.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menulist: [], // 菜单栏
      isCollapse: false,
      activePath: window.sessionStorage.getItem('activePath')
    }
  },
  created () {
    this.getMenuList()
  },
  methods: {
    logout () {
      // 退出
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenuList () {
      // 获取菜单栏的数据
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$message({
          message: '获取菜单数据失败',
          type: 'warning'
        })
      }
      this.menulist = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (activePath) { // 保存激活path路径
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background: #333744;
  .el-menu{
    border-right:none ;
  }
}
.el-main {
  background: #eaedf1;
}

.toggle-button{
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
}
</style>
